import api from "../../utils/api_util";
import baseUtil from "../../utils/base_util";

var app = getApp()
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        TabCur: 0,
        //数据参数
        hasNext:true,
        page: 1,
        size: 20,
        keyword: '',
        categoryId: '',
        type: '',
        orderField: 'is_recommend',
        orderBy: 'true',
        //排序
        orderList: [
            {
                'label': '推荐',
                'type': 'is_recommend',
            },
            {
                'label': '销量',
                'type': 'sale_volume',
            },
            {
                'label': '最新',
                'type': 'create_time',
            },
            {
                'label': '价格',
                'type': 'current_price',
                'isBy': '1',
                'orderBy': 'false',
            }],

        ShopList: [],
        topName:"",
        //搜索字段
        searchKeyWord:'',
    },
    onShow: function () {
        let that =this;
        this.data.topName =baseUtil.getCurrentUrlParam('name');
        this.getData();
        this.setData({
            ShopList:[],
            topName:that.data.topName
        })
    },

    //商品列表
    getData: function () {
        let that = this;
        let jumpType = baseUtil.getCurrentUrlParam('jumpType');
        if (jumpType == 'category') {
            that.setData({
                categoryId: baseUtil.getCurrentUrlParam('categoryId')
            })
        } else if (jumpType == 'search') {

            that.setData({
                keyword: baseUtil.getCurrentUrlParam('keyword')
            })
        } else if (jumpType == 'normal') {
            that.setData({
                type: baseUtil.getCurrentUrlParam('postType')
            })
        }
        api.wxRequest.apiRequest(that, api.urls.goodsList, {
            method: 'GET',
            data: {
                pageSize: that.data.size,    //每页条数
                pageNumber: that.data.page, //页码
                keyword: that.data.keyword,
                categoryId: that.data.categoryId,
                type: that.data.type,
                orderField: that.data.orderField,
                orderBy: that.data.orderBy,
            },
            success: function (res) {

                if(res.data.total > (that.data.size*that.data.page)){
                    that.data.hasNext = true;
                }else {
                    that.data.hasNext = false;
                    console.log('没有了')
                }

                if(res.data.list && res.data.list.length > 0){
                    that.data.ShopList = that.data.ShopList.concat(res.data.list);
                }

                console.log('ShopList:',that.data.ShopList);
                that.setData({
                    ShopList: that.data.ShopList,
                    hasNext: that.data.hasNext,
                })
            },
        })
    },
    //搜索字段
    searchInput:function(e){
        this.data.searchKeyWord =e.detail.value;

        this.setData({
            searchKeyWord:this.data.searchKeyWord,
        })
    },
    //搜索
    searchBtnEvent:function(e){
        let url =e.target.dataset.url,
            name =e.target.dataset.name;

           if(this.data.searchKeyWord){
               baseUtil.pageJump(url+'?jumpType='+name+'&keyword='+this.data.searchKeyWord,false);
           }
    },
    //赛选字段
    tabSelect(e) {
        let orderBy = 'false';
        let that = this;

        that.data.ShopList =[];
        if (e.target.dataset.isby && e.target.dataset.isby == 1) {
            if (e.target.dataset.orderby == 'false') {
                that.data.orderList[e.target.dataset.id].orderBy ='true';
                orderBy = 'true';
            } else {
                that.data.orderList[e.target.dataset.id].orderBy ='false';
                orderBy = 'false';
            }
        }
        console.log('type:~~',e.target.dataset.type);
        that.setData({
            size: 20,
            page: 1,
            TabCur: e.target.dataset.id,
            orderField: e.target.dataset.type,
            orderBy: orderBy,
            orderList: that.data.orderList,
            ShopList: that.data.ShopList,
        });

        this.getData();
    },

    showModal: function (e) {
        // this.data.TabCur == e.target.dataset.id ? '' : this.setData({
        //     TabCur: e.target.dataset.id
        // })
        // var showName = e.currentTarget.dataset.modal;
        // this.setData({
        //     modalName: showName
        // })
    },
    closeModal: function (e) {
        this.setData({
            modalName: null
        })
    },
    //页面跳转
    jumpPage (e){
        let id =e.target.dataset.id;
        baseUtil.pageJump('../ShopDetail/ShopDetail?id='+id,false);
        console.log('id：',id)
        // wx.navigateTo({
        //     url: "../ShopDetail/ShopDetail?id="+id
        // })
    },
    onReachBottom: function () {
        let that = this;
        if(that.data.hasNext){
            that.data.page++;
            that.getData();
        }
        this.setData({
            page: that.data.page,
        })
        // setTimeout(function () {
        //     that.setData({
        //         showLoading: false
        //     })
        // }, 2000)

    },
})